//
//  ViewController.m
//  HppleDemo
//
//  Created by Vytautas Galaunia on 11/25/14.
//
//

#import "ViewController.h"
//#import "TFHpple.h"
#import "HTMLParser.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSError *error = nil;
    NSString *html =
    @"<ul>"
    "<li><input type='image' name='input1' value='string1value' /></li>"
    "<li><input type='image' name='input2' value='string2value' /></li>"
    "</ul>"
    "<span class='spantext'><b>Hello World 1</b></span>"
    "<span class='spantext'><b>Hello World 2</b></span>";
    HTMLParser *parser = [[HTMLParser alloc] initWithString:html error:&error];
    
    if (error) {
        NSLog(@"Error: %@", error);
        return;
    }
    
    HTMLNode *bodyNode = [parser body];
    
    NSArray *inputNodes = [bodyNode findChildTags:@"input"];
    
    for (HTMLNode *inputNode in inputNodes) {
        if ([[inputNode getAttributeNamed:@"name"] isEqualToString:@"input2"]) {
            NSLog(@"%@", [inputNode getAttributeNamed:@"value"]); //Answer to first question
        }
    }
    
    NSArray *spanNodes = [bodyNode findChildTags:@"span"];
    
    for (HTMLNode *spanNode in spanNodes) {
        if ([[spanNode getAttributeNamed:@"class"] isEqualToString:@"spantext"]) {
            NSLog(@"%@", [spanNode rawContents]); //Answer to second question
        }
    }
    

    //    NSString *urlString = nil;
//    
//    urlString = @"www.baidu.com";
//    
    
    
//    NSData *htmlData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]];
//    
//    NSData *toHtmlData = [self toUTF8:htmlData];
//    
//    TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:toHtmlData];
//    
//    
//    NSArray *aArray = [xpathParser searchWithXPathQuery:@"//a"];
//    
//    if ([span count] > 0) {
//        
//        for (int i = 87; i < 102; i++) {
//            //从<a>的第82个开始取值，共获取15个值
//            TFHppleElement *aElement = [aArray objectAtIndex:i];
//            NSArray *aArr = [aElement children];
//            TFHppleElement *aEle = [aArr objectAtIndex:0];
//            NSArray *aChild = [aEle children];
//            TFHppleElement *aChildEle = [aChild objectAtIndex:0];
//            NSArray *aChildren = [aChildEle children];
//            NSString *aStr = [[aChildren objectAtIndex:0] content];
//            NSLog(@"aStr:%@",aStr);
//            NSDictionary *aAttributeDict = [aElement attributes];
//            NSLog(@"aAttributeDict:%@",aAttributeDict);
//            
//            //获取a中的属性值
//            NSString *hrefStr = [NSString stringWithFormat:@"http://www.xiyou.edu.cn%@",[aAttributeDict objectForKey:@"href"]];
//            NSLog(@"hrefStr:%@",hrefStr);
//            
//            [currentNewsArr addObject:aStr];
//            [currentHrefArr addObject:hrefStr];
//            
//        }
//  
//    }

}

//如果解析的网页不是utf8编码，如gbk编码，可以先将其转换为utf8编码再对其进行解析
//
//-(NSData *) toUTF8:(NSData *)sourceData {
//    CFStringRef gbkStr = CFStringCreateWithBytes(NULL, [sourceData bytes], [sourceData length], kCFStringEncodingGB_18030_2000, false);
//    
//    if (gbkStr == NULL) {
//        return nil;
//    } else {
//        NSString *gbkString = (NSString *)gbkStr;
//        //根据网页源代码中编码方式进行修改，此处为从gbk转换为utf8
//        NSString *utf8_String = [gbkString stringByReplacingOccurrencesOfString:@"META http-equiv="Content-Type" content="text/html; charset=GBK""
//                                                                     withString:@"META http-equiv="Content-Type" content="text/html; charset=UTF-8""];
//        
//        return [utf8_String dataUsingEncoding:NSUTF8StringEncoding];
//    }
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
